package com.shop2cn.iapi.pretrade.testcase;

import java.math.BigDecimal;
import java.util.HashMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.pretrade.paremeter.AdjustReq;
import com.shop2cn.iapi.pretrade.service.AdjustCall;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.AccountCallServiceV2;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqPretradeService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.AccountEntry;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

/**
 * 自动确认收货提醒通知FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Adjust {

	private static AdjustReq adjustReq;
	private static AdjustCall adjustCall;
	
	private static long virtualCoinGoodsOwnerId = ShenqiUserSet.virtualCoinGoodsOwnerId;
	private static AccountWapper accountWapper = new AccountWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代币调账");
	}

	@Before
	public void caseUp() {
		adjustReq = new AdjustReq();
		adjustCall = new AdjustCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(adjustCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify() {

		HashMap<String, Object> accountEntryMap = new HashMap<>();

		accountEntryMap.put("userId", adjustReq.getAgencyId());
		accountEntryMap.put("bizcode", "300053");
		accountEntryMap.put("originalNo", adjustReq.getGoodsOwnerId());
		accountEntryMap.put("accountType", "93");
		accountEntryMap.put("bizno", adjustReq.getUniqueCode());

		AccountEntry accountEntry = accountWapper.selectAccountEntryByMap(
				accountEntryMap).get(0);

		Logger.verifyEquals(adjustReq.getMemo(), accountEntry.getMemo(),
				"验证memo");
		Logger.verifyEquals(adjustReq.getOperator(),
				accountEntry.getOperator(), "验证Operator");
		DataUtil.verifyDecimalEquals(adjustReq.getAmount(),
				accountEntry.getAmount(), "验证Amount");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_001() {
		Logger.start(true, "代理商没有代币余额，正向调账");
		try {
			
			AgentProductBean agentProductBean = DGSQBuildProxyChainCallService
					.createIndependenceProxychain(
							(int) virtualCoinGoodsOwnerId, 1, false, false);

			adjustReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			adjustReq.setAgencyId(agentProductBean.getSellerId());
			adjustReq.setAmount(BigDecimal.ONE);
			adjustReq.setCsId(virtualCoinGoodsOwnerId);
			adjustReq.setMemo("正向调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(true, adjustCall.getSuccess(), "IsSuccess");
			
			this.verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_002() {
		Logger.start(true, "代理商有代币余额，正向调账");
		try {
			
			// 调账前账户金额
			BigDecimal amount = AccountCallServiceV2.getAccountBalance(
					502224180, 93); 

			adjustReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			adjustReq.setAgencyId(502224180L);
			adjustReq.setAmount(amount);
			adjustReq.setCsId(virtualCoinGoodsOwnerId);
			adjustReq.setMemo("正向调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(true, adjustCall.getSuccess(), "IsSuccess");
			
			this.verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_003() {
		Logger.start(true, "代理商有代币余额，负向调账为0");
		try {
			
			// 调账前账户金额
			BigDecimal amount = AccountCallServiceV2.getAccountBalance(
					502224180, 93); 

			adjustReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			adjustReq.setAgencyId(502224180L);
			adjustReq.setAmount(amount.negate());
			adjustReq.setCsId(virtualCoinGoodsOwnerId);
			adjustReq.setMemo("负向调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(true, adjustCall.getSuccess(), "IsSuccess");
			
			this.verify();
			
			SqPretradeService.virtualCoinCharge(virtualCoinGoodsOwnerId,
					502224180L, amount);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_004() {
		Logger.start(true, "代理商有代币余额，负向调账金额大于可用金额，调账失败");
		try {
			
			// 调账前账户金额
			BigDecimal amount = AccountCallServiceV2.getAccountBalance(
					502224180, 93); 

			adjustReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			adjustReq.setAgencyId(502224180L);
			adjustReq.setAmount(amount.add(new BigDecimal("0.01")).negate());
			adjustReq.setCsId(virtualCoinGoodsOwnerId);
			adjustReq.setMemo("负向调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(false, adjustCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("代理货币金额不能够调账为负", adjustCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_005() {
		Logger.start(true, "代理商有代币余额，调账金额为0，调账失败");
		try {
			
			// 调账前账户金额
			adjustReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			adjustReq.setAgencyId(502224180L);
			adjustReq.setAmount(BigDecimal.ZERO);
			adjustReq.setCsId(virtualCoinGoodsOwnerId);
			adjustReq.setMemo("调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(false, adjustCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("Amount必须是大于0的数。", adjustCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_006() {
		Logger.start(true, "客服没有权限调账，调账失败");
		try {
			
			// 调账前账户金额
			adjustReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			adjustReq.setAgencyId(502224180L);
			adjustReq.setAmount(BigDecimal.ZERO);
			adjustReq.setCsId(4321L);
			adjustReq.setMemo("调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(false, adjustCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("无权操作", adjustCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_007() {
		Logger.start(true, "货头给其他代理调账，调账失败");
		try {
			
			// 调账前账户金额
			adjustReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			adjustReq.setAgencyId(502101120L);
			adjustReq.setAmount(BigDecimal.ONE);
			adjustReq.setCsId(virtualCoinGoodsOwnerId);
			adjustReq.setMemo("调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(false, adjustCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("代理商[ 502101120 ]不存在", adjustCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Adjust_008() {
		Logger.start(true, "非独立云主商户调账，调账失败");
		try {
			
			AgentProductBean agentProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) 500002490, 1, false);
			
			// 调账前账户金额
			adjustReq.setGoodsOwnerId(500002490L);
			adjustReq.setAgencyId(agentProductBean.getSellerId());
			adjustReq.setAmount(BigDecimal.ONE);
			adjustReq.setCsId(500002490L);
			adjustReq.setMemo("调账");
			adjustReq.setOperator("operato");
			adjustReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			adjustCall.setData(adjustReq);
			adjustCall.callService();

			Logger.verifyEquals(false, adjustCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("独立云货头[ 500002490 ]不存在", adjustCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
